package safrain.nodelet.template.field;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import safrain.nodelet.structure.FieldInfo;

public abstract class ValueField<T extends Annotation> {
	public FieldInfo fieldInfo;
	public Field field;

	public FieldInfo getFieldInfo() {
		return fieldInfo;
	}

	public void set(Object obj) {
		try {
			set0(obj);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	protected abstract void set0(Object obj) throws Exception;

	public abstract void setValue(Object value);

	public static <T extends Annotation> ValueField<T> getValueField(
			FieldInfo fieldInfo) {
		ValueField<T> valueField = null;
		switch (fieldInfo.fieldType) {
		case INT:
			valueField = new IntField<T>();
			break;
		case LONG:
			valueField = new LongField<T>();
			break;
		case BYTE:
			valueField = new ByteField<T>();
			break;
		case CHAR:
			valueField = new CharField<T>();
			break;
		case BOOLEAN:
			valueField = new BooleanField<T>();
			break;
		case SHORT:
			valueField = new ShortField<T>();
			break;
		case FLOAT:
			valueField = new FloatField<T>();
			break;
		case DOUBLE:
			valueField = new DoubleField<T>();
			break;
		case REFERENCE:
			valueField = new ReferenceField<T>();
			break;
		}
		valueField.fieldInfo = fieldInfo;
		valueField.field = fieldInfo.field;
		return valueField;
	}
}
